package com.tuanzi.loan.business.repository.system.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanAnnouncement.LOAN_ANNOUNCEMENT;

import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.tuanzi.loan.business.repository.system.custom.AnnouncementRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.system.AnnouncementSearchRequest;
import com.tuanzi.loan.vo.response.system.AnnouncementSearchResponse;

/**
 * @author Ray 2017/09/21
 */
@SuppressWarnings({ "rawtypes" })
public class AnnouncementRepositoryImpl extends JOOQBaseRepository implements AnnouncementRepositoryCustom {

	@Override
	protected TableImpl getTable() {
		return LOAN_ANNOUNCEMENT;
	}

	@Override
	public Page<AnnouncementSearchResponse> list(AnnouncementSearchRequest searchRequest, Pageable pageable) {
		contains(LOAN_ANNOUNCEMENT.TITLE, searchRequest.getTitle());
		contains(LOAN_ANNOUNCEMENT.CONTENT, searchRequest.getContent());
		eq(LOAN_ANNOUNCEMENT.SEND_EMAIL, searchRequest.getSendEmail());
		le(LOAN_ANNOUNCEMENT.START_TIME, searchRequest.getStartTime());
		gt(LOAN_ANNOUNCEMENT.END_TIME, searchRequest.getEndTime());
		return search(pageable, (record) -> {
			AnnouncementSearchResponse response = new AnnouncementSearchResponse();
			response.setId(record.get(LOAN_ANNOUNCEMENT.ID));
			response.setTitle(record.get(LOAN_ANNOUNCEMENT.TITLE));
			response.setSendEmail(record.get(LOAN_ANNOUNCEMENT.SEND_EMAIL) == null ? null : record.get(LOAN_ANNOUNCEMENT.SEND_EMAIL) == 1);
			response.setContent(record.get(LOAN_ANNOUNCEMENT.CONTENT));
			response.setStartTime(record.get(LOAN_ANNOUNCEMENT.START_TIME));
			response.setEndTime(record.get(LOAN_ANNOUNCEMENT.END_TIME));
			return response;
		});
	}
}
